package com.xiaozhou.leetCode.leetCode100;


public class LeetCode8 {

    public static int myAtoi(String s) {
        String trim = s.trim();
        if("".equals(trim)){
            return 0;
        }
        String s1 = "";
        if (trim.charAt(0) == '-' || trim.charAt(0) == '+' || (trim.charAt(0) >='0' && trim.charAt(0) <='9')){
            s1 += trim.charAt(0);
        }else{
            return 0;
        }
        for (int i = 1; i < trim.length(); i++) {
            if (trim.charAt(i) >='0' && trim.charAt(i) <='9'){
                if (Long.valueOf(s1 + trim.charAt(i)) > Integer.MAX_VALUE){
                    return Integer.MAX_VALUE;
                }else if (Long.valueOf(s1 + trim.charAt(i)) < Integer.MIN_VALUE){
                    return Integer.MIN_VALUE;
                }
                s1  += trim.charAt(i);
            }else {
                break;
            }
        }
        if ("".equals(s1) || "-".equals(s1) || "+".equals(s1)){
            return 0;
        }
        return Integer.valueOf(s1);
    }

    public static void main(String[] args) {
        int i = myAtoi("+-42");
        System.out.println(i);
    }
}
